var defaultStyleOptions = {
	strokeColor: "red", //边线颜色。
	fillColor: "red", //填充颜色。当参数为空时，圆形将没有填充效果。
	strokeWeight: 2, //边线的宽度，以像素为单位。
	strokeOpacity: 0.8, //边线透明度，取值范围0 - 1。
	fillOpacity: 0.1, //填充的透明度，取值范围0 - 1。
	strokeStyle: 'solid' //边线的样式，solid或dashed。
};
(function($) {
	$.extend({
		initBMap: function(options) {
			var defaults = {
				id: 'container', //容器默认Id
				centerPoint: new BMap.Point(114.237483, 30.643606), //new BMap.Point(114.142696, 30.626291); 创建点坐标  
				minZoom: 13, //左侧缩放最小可视
				maxZoom: 18, //左侧放大最大可视
				mapDefaultZoom: 18, //初始化地图级别
				enableScrollWheelZoom: true, //是否滚轮滚动
				drawingModes: [], //绘制按钮组
				getDrawingValue: function() {} //回调函数
			};

			_options = $.extend(defaults, options);
			var paramars = _options;

			var map = new BMap.Map(paramars.id, {
				minZoom: paramars.minZoom,
				maxZoom: paramars.maxZoom
			});

			var point = paramars.centerPoint;
			//实例化鼠标绘制工具
			var drawingManager = new BMapLib.DrawingManager(map, {
				isOpen: false, //是否开启绘制模式
				enableDrawingTool: true, //是否显示工具栏
				drawingToolOptions: {
					anchor: BMAP_ANCHOR_TOP_RIGHT, //位置
					offset: new BMap.Size(5, 5), //偏离值
					scale: 0.8, //工具栏缩放比例
					drawingModes: paramars.drawingModes//绘制类型
				},
				markerOptions: defaultStyleOptions, //点的样式
				circleOptions: defaultStyleOptions, //圆的样式
				polylineOptions: defaultStyleOptions, //线的样式
				polygonOptions: defaultStyleOptions, //多边形的样式
				rectangleOptions: defaultStyleOptions //矩形的样式
			});
			var overlays = [];

			//添加鼠标绘制工具监听事件，用于获取绘制结果
			drawingManager.addEventListener('overlaycomplete', function(e) {

				var hiddenobj = {
					drawingModel: "",//绘制类型
					points: []//点集合
				};

				if(overlays.length > 0) {
					for(var i = 0; i < overlays.length; i++) {
						map.removeOverlay(overlays[i]);
					}
				}

				overlays.push(e.overlay);

				switch(e.drawingMode) {
					case "marker": //BMAP_DRAWING_MARKER
						//hidobj += e.overlay.getPosition().lng + ',' + e.overlay.getPosition().lat + ",marker";
						hiddenobj.drawingModel = "marker";
						hiddenobj.points.push(new BMap.Point(e.overlay.getPosition().lng, e.overlay.getPosition().lat));
						break;
					case "circle": //BMAP_DRAWING_CIRCLE
						//hidobj += e.overlay.getRadius() + "," + e.overlay.getCenter().lat + "," + e.overlay.getCenter().lng + ",circle";
						hiddenobj.drawingModel = "circle";
						hiddenobj.points.push(new BMap.Point(e.overlay.getCenter().lng, e.overlay.getCenter().lat));
						hiddenobj.radius = e.overlay.getRadius();
						break;
					case "polyline": //BMAP_DRAWING_POLYLINE
					case "rectangle": //BMAP_DRAWING_RECTANGLE
					case "polygon": //BMAP_DRAWING_POLYGON
						hiddenobj.drawingModel = e.drawingMode;
						
						for(i = 0; i < e.overlay.getPath().length; i++) {
							//hidobj += e.overlay.getPath()[i].lat + "," + e.overlay.getPath()[i].lng + ",";
							hiddenobj.points.push(new BMap.Point(e.overlay.getPath()[i].lng, e.overlay.getPath()[i].lat));
						}

						break;
					default:
						break;
				}

				paramars.getDrawingValue(hiddenobj);

			});

			map.centerAndZoom(point, paramars.mapDefaultZoom); //初始化地图，设置中心点坐标和地图级别  
			//添加地图类型控件
			//			map.addControl(new BMap.MapTypeControl({
			//				mapTypes: [BMAP_NORMAL_MAP, BMAP_HYBRID_MAP]
			//			}));
			map.enableScrollWheelZoom(paramars.enableScrollWheelZoom); //开启鼠标滚轮缩放
			map.addControl(new BMap.NavigationControl()); //添加标准地图控件(左上角的放大缩小左右拖拽控件)

			$('.Clear_All').on('click', function() {
				map.clearOverlays();
				//初始化地图
				 map = $.initBMap({
					drawingModes: [
					  BMAP_DRAWING_MARKER,
					  BMAP_DRAWING_POLYLINE,
					  BMAP_DRAWING_POLYGON,
					  BMAP_DRAWING_CIRCLE,
					  BMAP_DRAWING_RECTANGLE,
					],
					getDrawingValue: function (hiddenobj) {
					  var hiddenStr = JSON.stringify(hiddenobj)
					  $('#hidParam').val(hiddenStr)
					},
				  })
			});
			return map;
		},
		//添加自定义覆盖物
		//Bmap：百度map对象
		//coverObject：覆盖物对象coverObject:{drawingModel: "",points: [],radius:""};
		addCoverLayer: function(Bmap, coverObject, mapDefaultZoom,centerPoint) {
			addGridCoverObj(Bmap, coverObject, mapDefaultZoom,centerPoint);
		},
		clearOverlays:function(Bmap){
			Bmap.clearOverlays();
		}
	});

	//此处points为数组[{"lng":114.159115804558,"lat":30.7584255641157}]
	var formateJsonToArrayPoint = function(points) {

		var newPointArr = [];

		$.each(points, function(index, item) {
			var point = new BMap.Point(item.lng, item.lat);

			newPointArr.push(point);
		});

		return newPointArr;
	};

	//添加覆盖物
	//覆盖物对象coverObject:{drawingModel: "",points: [],radius:""};
	var addGridCoverObj = function(map, coverObject, zoomSize,centerPoint) {
		if(centerPoint==undefined){
			centerPoint=new BMap.Point(114.237483, 30.643606);
		}
		if(coverObject.points != null & coverObject.points != "") {
			var overlays = [];
			var bMapPoints = formateJsonToArrayPoint(coverObject.points);

			switch(coverObject.drawingModel) {
				case "marker":
					var point = bMapPoints[0];
					var marker = new BMap.Marker(point); // 创建点
					map.addOverlay(marker); //增加点
					map.centerAndZoom((point), zoomSize);
					overlays.push(marker);
					break;
				case "polyline":
					var polyline = new BMap.Polyline(bMapPoints, defaultStyleOptions); //创建折线
					map.addOverlay(polyline); //增加折线
					map.centerAndZoom(centerPoint, zoomSize);
					overlays.push(polyline);
					break;
				case "circle":
					var point = bMapPoints[0];
					var circle = new BMap.Circle(point, coverObject.radius, defaultStyleOptions); //创建圆
					map.addOverlay(circle); //增加圆
					map.centerAndZoom(centerPoint, zoomSize);
					overlays.push(circle);
					break;
				case "polygon":
					var polygon = new BMap.Polygon(bMapPoints, defaultStyleOptions); //创建多边形
					map.addOverlay(polygon); //增加多边形
					map.centerAndZoom(centerPoint, zoomSize);
					overlays.push(polygon);
					break;
				case "rectangle":
					var rectangle = new BMap.Polygon(bMapPoints, defaultStyleOptions); //创建矩形
					map.addOverlay(rectangle); //增加矩形
					map.centerAndZoom(centerPoint, zoomSize);
					overlays.push(rectangle);
					break;
				default:
					break;
			}

		}
	};

})(jQuery);